<?php

namespace App\V3\V3_2_7\Services;

use App\Services\CommonService;
use App\V3\V3_2_7\Models\AdminAddendumModel;
use App\V3\V3_2_7\Models\AdminModel;
use App\V3\V3_2_7\Models\OperMainAccountModel;
use Illuminate\Support\Facades\Log;

class OperMainAccountService
{
    /**
     *操作主账号使用负数库存
     * Author: zlf
     * Created at 2024/7/10 9:42
     * @param $data
     * @return array
     */
    public static function edit($data)
    {
        try {
            $mainArr = [];
            if (!empty($data['main_phone'])) {
                $mainArr[] = $data['main_phone'];
                if (strpos($data['main_phone'], ',') !== false) {
                    $mainArr = explode(',', $data['main_phone']);
                }

                $inserData = [];
                foreach ($mainArr as $mVal) {
                    $adminInfo = AdminModel::query()->where([
                        'username' => $mVal
                    ])->first();
                    if (empty($adminInfo))
                        throw new \RuntimeException('账号：' . $mVal . '，无账号信息！');
                    if (!empty($adminInfo['delete_flag']))
                        throw new \RuntimeException('账号：' . $mVal . '，已删除！');
                    if (empty($adminInfo['is_active']))
                        throw new \RuntimeException('账号：' . $mVal . '，已禁用！');
                    if (!empty($adminInfo['parent_id']))
                        throw new \RuntimeException('账号：' . $mVal . '，不是主账号！');
                    if (empty(AdminAddendumModel::query()
                        ->where([
                            'main_id' => $adminInfo['id']
                        ])->value('is_vip'))) {
                        throw new \RuntimeException('账号：' . $data['main_phone'] . '，不是VIP账号！');
                    }
                    if (OperMainAccountModel::query()
                        ->where([
                            'main_id' => $adminInfo['id'],
                            'delete_flag' => 0
                        ])->exists()) {
                        throw new \RuntimeException('账号：' . $mVal . '，已存在！');
                    }
                    $inserData[] = [
                        'main_phone' => $mVal,
                        'main_id' => $adminInfo['id']
                    ];
                }
            }
            if (!empty($inserData)) {
                OperMainAccountModel::query()->insert($inserData);
            } else {
                throw new \RuntimeException('无数据！');
            }
            return ['code' => 0, 'msg' => 'success', 'data' => ''];
        } catch (\Exception $e) {
            Log::debug('操作主账号使用负数库存异常：' . var_export($e->getMessage(), true));
            return ['code' => 1, 'msg' => $e->getMessage(), 'data' => ''];
        }
    }

    /**
     *
     * Author: zlf
     * Created at 2024/7/10 11:28
     * @param $id
     * @return mixed
     */
    public static function del($id)
    {
        return OperMainAccountModel::query()->where([
            'id' => $id
        ])->update(['delete_flag' => 1]);
    }

    public static function info($data)
    {
        return OperMainAccountModel::query()
            ->select('id','main_phone')
            ->where('delete_flag', 0)
            ->orderBy('id', 'DESC')
            ->paginate($data['pageSize'], ['*'], '', $data['currentPage'])->toArray();
    }
}
